package com.rhit.rosehack.engine;

import java.util.ArrayList;


//Generates a map.
public class MapGenerator {
	
	private char[][] map;
	private ArrayList<RoomData> rooms;
	
	private class RoomData{
		public int x,y,length,height;
	}
	
	
	public MapGenerator()
	{
		this.rooms = new ArrayList<MapGenerator.RoomData>();
	};
	
	public char[][] getMap()
	{
		this.rooms = new ArrayList<MapGenerator.RoomData>();
		this.map = new char[40][90];
		
		for(int x = 0; x < 40; x ++)
			for(int y = 0; y < 90; y++)
				this.map[x][y] = '#';
		
		makeRooms();
		makeCorridors();
		removeBadWalls();
		placeStairs();
		
		
		return map;
	}

	
	private void placeStairs() {
		
		boolean placed = false;
				
				while(!placed)
				{
					int x = (int)(Math.random()*90);
					int y = (int)(Math.random()*40);
					if(this.map[y][x] == '.')
					{
						this.map[y][x] = '<'; 
						placed = true;
					}
					
				}
		placed = false;
				
				while(!placed)
				{
					int x = (int)(Math.random()*90);
					int y = (int)(Math.random()*40);
					if(this.map[y][x] == '.')
					{
						this.map[y][x] = '>'; 
						placed = true;
					}
					
				}
		
	}

	private void makeCorridors() {
		for(int x = 0; x < this.rooms.size()-1; x ++)
		{			
			RoomData temp = this.rooms.get(x);
			RoomData temp2 = this.rooms.get(x+1);
			
			if(temp.x>temp2.x)
			{
				
				if(temp.y > temp2.y)
				{
					
					//THINK right
					for(int i = temp2.x; i <= temp.x; i ++)
					{
						this.map[temp2.y][i] = '.';
					}
					for(int j = temp2.y; j <= temp.y; j++)
					{
						this.map[j][temp.x] = '.';
					}
					
				}
				
				else
					
					//Think so
					for(int i = temp2.x; i <= temp.x; i ++)
					{
						this.map[temp2.y][i] = '.';
					}
					for(int j = temp.y; j <= temp2.y; j++)
					{
						this.map[j][temp.x] = '.';
					}
					
				
			}
			else
			{
				
				if(temp.y > temp2.y)
				{
					
					
					for(int i = temp.x; i <= temp2.x; i ++)
					{
						this.map[temp.y][i] = '.';
					}
					for(int j = temp2.y; j <= temp.y; j++)
					{
						this.map[j][temp2.x] = '.';
					}
					
				}
				
				else
					for(int i = temp.x; i <= temp2.x; i ++)
					{
						this.map[temp.y][i] = '.';
					}
					for(int j = temp.y; j <= temp2.y; j++)
					{
						this.map[j][temp2.x] = '.';
					}
					
				
			}
			
			
			
			
			
			
		}
		
	}

	private void removeBadWalls() {
		boolean remove = true;
		for(int x = 1; x < this.map[0].length-1; x++)
		{
			for(int y = 1; y < this.map.length-1; y++)
			{
				if(this.map[y-1][x-1] == '.' ||
					this.map[y-1][x] == '.' ||
					this.map[y-1][x+1] == '.' ||
					this.map[y][x-1] == '.' ||
					this.map[y][x+1] == '.' ||
					this.map[y+1][x-1] == '.' ||
					this.map[y+1][x] == '.' ||
					this.map[y+1][x+1] == '.')
					remove = false;
				
				
				
				
				
				
				
				
				if(remove)
				{
					this.map[y][x] = ' ';
				}
				remove = true;
				
			}
			
			
			
		}
		
	}

	//Creates random rooms in the map
	private void makeRooms() {
		
		for(int x = 0; x < 5; x ++)
		{
			RoomData r = new RoomData();
			r.x = (int)(Math.random() * 79)+3;
			r.y = (int)(Math.random() * 30)+3;
			
			r.length = (int)(Math.random() * 9)+4;
			r.height = (int)(Math.random() * 9)+4;
			
			while(r.x + r.length > 88 || r.y+r.height > 38)
			{
			
				r.x = (int)(Math.random() * 79)+3;
				r.y = (int)(Math.random() * 30)+3;
				r.length = (int)(Math.random() * 9)+4;
				r.height = (int)(Math.random() * 9)+4;
				
			}
			
			
			for(int i = r.x; i < r.x + r.length; i ++)
				for(int j = r.y; j < r.y + r.height; j++)
				{
					this.map[j][i] = '.';
				}
			
			this.rooms.add(r);
		}	
		
			
			
		
		
		
		
		
		
	}

	

}